﻿/*************************************************************************
 *  Copyright (C), 2017-2018, YangTao tech. Co., Ltd.
 *  FileName: DrawingPath.cs
 *  Author: YangTao   Version: 1.0   Date: 1/12/2017
 *  Email:YangTao_Unity3D@163.com
 *  Version Description:
 *    等分多个点间线段，取各点坐标。以此制作路径标识
 *  File Description:
 *    
 *  Class List:
 *    <ID>           <name>             <description>
 *     1.         DrawingPath             
 *  Function List:
 *    <class ID>     <name>             <description>
 *     1.
 *  History:
 *    <ID>    <author>      <time>      <version>      <description>
 *     1.     YangTao     1/12/2017       1.0        Build this file.
 *************************************************************************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DrawingPath : MonoBehaviour
{
    /// <summary>路径点数组</summary>
    public Transform[] m_pathPointArry;
    /// <summary>分段数</summary>
    public int m_segmentNum;
    /// <summary>路径</summary>
    public GameObject m_path;
    /// <summary>路径点是否实例化完毕</summary>
    bool m_isInstantiate;
    /// <summary>是否开始高亮显示路径</summary>
    bool m_isStart;
    /// <summary>显示下一路径标识等待时间</summary>
    float m_waitTime;
    /// <summary>存放路径标识List</summary>
    List<GameObject> m_pathList;
    int j;

    void Start()
    {
        Init();
    }

    void Update()
    {
        if (!m_isInstantiate)
        {
            SetPoint();
        }
        HighlightPath();
    }

    /// <summary>初始化</summary>
    void Init()
    {
        m_isInstantiate = false;
        m_isStart = true;
        m_waitTime = 0;
        m_pathList = new List<GameObject>();
        j = 0;
    }

    /// <summary>设置路径</summary>
    void SetPoint()
    {
        Vector3[] directionVectorArry;//方向向量数组
        float[] distanceArry;//距离数组 
        float distance=0;//总距离
        directionVectorArry = new Vector3[m_pathPointArry.Length - 1];
        distanceArry = new float[m_pathPointArry.Length - 1];
        int m = 0;
        while (m< m_pathPointArry.Length-1)
        {
            distanceArry[m] = Vector3.Distance(m_pathPointArry[m + 1].position, m_pathPointArry[m].position);
            directionVectorArry[m] = (m_pathPointArry[m+1].position - m_pathPointArry[m].position).normalized;
            distance += distanceArry[m];
            m++;
        }       
        int i = 0;
        int k = 0;
        while (k<m)
        {                  
            //路径点到生成的路径的距离小于路径点到下一路径点的距离              
            while (Vector3.Distance(m_pathPointArry[k].position, m_pathPointArry[k].position + directionVectorArry[k] * distance / m_segmentNum * i)<Vector3.Distance(m_pathPointArry[k].position,m_pathPointArry[k+1].position))
            {
                GameObject obj;
                obj=Instantiate(m_path, m_pathPointArry[k].position + directionVectorArry[k] * distance / m_segmentNum * i, Quaternion.identity)as GameObject;
                obj.transform.LookAt(m_pathPointArry[k]);//调整方向朝向下一路径点
                m_pathList.Add(obj);
                i++;
            }
            k++;
            i = 0;                   
        }
        m_isInstantiate = true;
    }

    /// <summary>高亮显示路径</summary>
    void HighlightPath()
    {
        if (m_isStart)
        {
            if (m_waitTime > 0.05f)
            {
                m_pathList[j].transform.GetChild(0).gameObject.SetActive(true);
                j++;
                m_waitTime = 0;
                if (j == m_pathList.Count)
                {
                    j = 0;
                    for (int i = 0; i < m_pathList.Count; i++)
                    {
                        m_pathList[i].transform.GetChild(0).gameObject.SetActive(false);
                    }
                }
            }
            else
            {
                m_waitTime += Time.deltaTime;
            }
        }
    }

}